<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>09_计算器练习</title>
</head>
<body>
    <input type="text" placeholder="请输入数字1" id="i1">
    <input type="text" placeholder="请输入数字2" id="i2">
    <button onclick="calc('+')">加</button>
    <button onclick="calc('-')">减</button>
    <button onclick="calc('*')">乘</button>
    <button onclick="calc('/')">除</button>
    <hr>
    <h2>运算结果为：<span></span></h2>
    <script>
      function calc(o){
        let n1 = document.querySelector('#i1').value;
        let n2 = document.querySelector('#i2').value;
        let spanE = document.querySelector('span')
        n1 = parseFloat(n1);
        n2 = parseFloat(n2);
        switch (o){
          case '+':
            spanE.innerHTML = n1 + n2;
            break;
          case '-':
            spanE.innerHTML = n1 - n2;
            break;
          case '*':
            spanE.innerHTML = n1 * n2;
            break;
          case '/':
            console.log(n2);
            if (n2!=0 && !isNaN(n2)){
              spanE.innerHTML = n1 / n2;
            }else alert('除数不能为0！');
        }
      }
    </script>
</body>
</html>